package com.hs.li.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hs.dto.Constants;
import com.hs.dto.PageBean;
import com.hs.dto.ResultBean;
import com.hs.li.pojo.CarDriver;
import com.hs.li.service.ICarDriverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.sql.Driver;
import java.util.List;

/**
 * <p>
 * 司机管理 前端控制器
 * </p>
 *
 * @author lhj
 * @since 2022-06-13
 */
@CrossOrigin
@RestController
@RequestMapping("/carDriver")
public class CarDriverController {

    @Autowired
    private ICarDriverService carDriverService;


    @GetMapping
    public ResultBean queryDriver(Integer currentPage, Integer pageSize) {

        IPage page = new Page(currentPage, pageSize);

        IPage p = carDriverService.page(page);
        List records = p.getRecords();
        Long total = p.getTotal();

        if (!CollectionUtils.isEmpty(records)) {
            PageBean<Driver> driverPageBean = new PageBean<>();
            driverPageBean.setList(records);
            driverPageBean.setTotal(total.intValue());

            ResultBean resultBean = new ResultBean();
            resultBean.setCode(Constants.SUCCESS_CODE);
            resultBean.setMsg("查询成功！");
            resultBean.setData(driverPageBean);

            return resultBean;
        }
        return ResultBean.ERROR("查询失败！");


    }

    @RequestMapping("/search")
    public ResultBean searchDriverByCarPlate(Integer carNumber) {
        System.out.println("carNumber = " + carNumber);

        if(carNumber > 202206130){
            List<CarDriver> carDrivers = carDriverService.searchDriverByCarNumber(carNumber);

            ResultBean resultBean = new ResultBean();
            resultBean.setData(carDrivers);
            resultBean.setCode(Constants.SUCCESS_CODE);
            resultBean.setMsg("查询司机成功！");

            return resultBean;
        }
        return ResultBean.ERROR("车辆编号对应的司机为空！");

    }

}
